package vn.student;

import java.util.ArrayList;
import vn.student.controller.ViewEditClassDetailController;
import vn.student.mylib.ClassType;
import vn.student.mylib.ClassTypeInfo;
import vn.student.mylib.Constants;
import vn.student.mylib.Contact;
import vn.student.mylib.DateDateDialog;
import vn.student.mylib.ListClassTypeAdapter;
import vn.student.mylib.ListContactAdapter;
import vn.student.mylib.TextInputDialog;
import vn.student.mylib.TimeTimeDialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

	/*
	
		Class name: ViewClassDetailEditor.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity dung de xem 
			thong tin chi tiet mot lop mon hoc va cho phep chinh sua thong tin.
		
	*/

public class ViewEditClassDetailEditor extends Activity implements View.OnClickListener{
	String id = null;
	Activity act = this;
	ViewEditClassDetailController controller = null;
	boolean isCreateNew = false;
	public AlertDialog chooseTypeDialog = null;
	boolean isEditLocation = false;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.isCreateNew = isCreateNew;
		setContentView(R.layout.edit_class_detail);
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickCancelButton();
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickSaveButton();
			}
		});
		RelativeLayout btn = (RelativeLayout) this.findViewById(R.id.classNameEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editClassName();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.durationEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editDuration();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.locationEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editLocation();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.timeEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editTime();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.repeatEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editRepeat();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.typeEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickEditTypeBtn();
			}
		});
		btn = (RelativeLayout) this.findViewById(R.id.instructorEditBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickEditInstructorBtn();
			}
		});
		Intent intent = this.getIntent();
		id = intent.getExtras().getString("id");
		isCreateNew = intent.getExtras().getBoolean("isCreateNew");
		
		if(isCreateNew){
			String courseID = intent.getExtras().getString("courseID");
			controller = new ViewEditClassDetailController(this,id,isCreateNew,courseID);
			controller.showClassDetailEditor();
		}
		else{
			controller = new ViewEditClassDetailController(this,id,isCreateNew);
			controller.showClassDetailEditor(id);
		}
		
	}
	private void editRepeat(){
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.repeat_type_menu, null); 
		AlertDialog.Builder builder = new AlertDialog.Builder(act); 
		builder.setTitle("Choose repeat type");
		Button btn = (Button) view.findViewById(R.id.noneRepeatBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				controller.viewNoneRepeatView();
				chooseTypeDialog.dismiss();
				chooseTypeDialog = null;
			}
		});
		btn = (Button) view.findViewById(R.id.repeatWeeksBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				controller.viewRepeatWeeksView();
				chooseTypeDialog.dismiss();
				chooseTypeDialog = null;
			}
		});
		btn = (Button) view.findViewById(R.id.repeatWorkingDaysBtn);
		btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				controller.viewRepeatWorkingDays();
				chooseTypeDialog.dismiss();
				chooseTypeDialog = null;
			}
		});
		builder.setView(view);
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		chooseTypeDialog = builder.create();
		chooseTypeDialog.show();
	}
	private void editTime(){
		TimeTimeDialog.viewDialog("Time",controller.classInfo.timeStart.split(":"),controller.classInfo.timeEnd.split(":"), act, new TimeTimeDialogOnOkClickListener());
	}
	private void editLocation(){
		this.isEditLocation = true;
		TextInputDialog.viewDialog("Input location",controller.classInfo.location, act, new InputTextDialogOnOkClickListener());
	}
	private void editClassName(){
		TextInputDialog.viewDialog("Input class name",controller.classInfo.className, act, new InputTextDialogOnOkClickListener());
	}
	private void editDuration(){
		String[] dateStart = controller.classInfo.dateStart.split("-");
		if(dateStart.length!=3)
			return;
		String[] dateEnd = controller.classInfo.dateEnd.split("-");
		if(dateEnd.length!=3)
			return;
		DateDateDialog.viewDialog("Class duration",dateStart,dateEnd, act, new DateDateDialogOnOkClickListener());
	}
	private void onClickEditInstructorBtn(){
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.instructor_list_view, null); 
		AlertDialog.Builder builder = new AlertDialog.Builder(act); 
		builder.setTitle("Choose Instructor"); 
		builder.setView(view);
		builder.setPositiveButton("Add new Intructor", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				addNewInstructor();
				
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		ArrayList<Contact> instructorList = controller.getInstructor();
		ListView listClassType = (ListView) view.findViewById(R.id.contactListView);
		listClassType.setAdapter(new ListContactAdapter(this,view,instructorList));
		listClassType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ListContactAdapter adapter = (ListContactAdapter) arg0.getAdapter();
				ArrayList<Contact> list = adapter.list;
				controller.editInstructor(list.get(arg2).id,list.get(arg2).name);
				chooseTypeDialog.dismiss();
				chooseTypeDialog = null;
			}
		});
		//get the dialog 
		chooseTypeDialog = builder.create();
		chooseTypeDialog.show();
		//chooseTypeDialog.dismiss();
	}
	private void addNewInstructor(){
		Intent intent = new Intent(act,ViewContactDetail.class);
		Bundle bundle = new Bundle();
		bundle.putString("id","-1");
		bundle.putBoolean("isEdit", true);
		bundle.putBoolean("isInstructor", true);
		bundle.putBoolean("isCreateNew", true);
		intent.putExtras(bundle);
		act.startActivityForResult(intent,Constants.VIEW_ADD_NEW_INSTRUCTOR_EDITOR);
	}
	public void onClickEditTypeBtn(){
		LayoutInflater li = LayoutInflater.from(act); 
		View view = li.inflate(R.layout.class_type_list_view, null); 
		AlertDialog.Builder builder = new AlertDialog.Builder(act); 
		builder.setTitle("Choose class type"); 
		builder.setView(view);
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		ArrayList<ClassTypeInfo> classType = new ArrayList<ClassTypeInfo>();
		int m = ClassType.codeArr.length;
		for(int i=0;i<m;i++){
			classType.add(new ClassTypeInfo(ClassType.nameArr[i],ClassType.codeArr[i]));
		}
		if(classType.size()<=0)
			return; 
		ListView listClassType = (ListView) view.findViewById(R.id.classTypeListView);
		listClassType.setAdapter(new ListClassTypeAdapter(this,view,classType));
		listClassType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ListClassTypeAdapter adapter = (ListClassTypeAdapter) arg0.getAdapter();
				ArrayList<ClassTypeInfo> list = adapter.list;
				controller.editType(list.get(arg2).classTypeCode);
				chooseTypeDialog.dismiss();
				chooseTypeDialog = null;
			}
		});
		//get the dialog 
		chooseTypeDialog = builder.create();
		chooseTypeDialog.show();
		//chooseTypeDialog.dismiss();
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	private void onClickCancelButton(){
		this.setResult(Activity.RESULT_CANCELED);
		this.finish();
	}
	private void onClickSaveButton(){
		if(controller.saveClassInfo()){
			this.setResult(Activity.RESULT_OK);
			this.finish();
		}
		else{
			
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode,Intent data) {
	    if (requestCode==Constants.ViewEditClassDetailEditorRequestCode) {
	      if (resultCode==RESULT_OK) {
	    	  String title = data.getExtras().getString("title");
	    	  String newValue = data.getExtras().getString("newValue");
	    	  TextView label;
	    	  if(title.equals("className"))
	    		  controller.classInfo.className=newValue;
	    	  else if(title.equals("location"))
	    		  controller.classInfo.location=newValue;
	    	  controller.showClassDetailEditor(id);
	      }
	    }
	    if (requestCode==Constants.VIEW_ADD_NEW_INSTRUCTOR_EDITOR) {
		      if (resultCode==RESULT_OK) {
		    	  String insId = data.getExtras().getString("instructorId");
		    	  String insName = data.getExtras().getString("instructorName");
		    	  controller.editInstructor(insId, insName);
		      }
	    }
	  }
	class DateDateDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			String tmp;
			DatePicker date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateStartPicker);
			String dateStartStr = "";
			dateStartStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateStartStr += tmp;
			
			date = (DatePicker) ((AlertDialog)dialog).findViewById(R.id.dateEndPicker);
			
			String dateEndStr = "";
			dateEndStr = String.valueOf(date.getYear()) + "-";
			tmp = String.valueOf(date.getMonth()+1);
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp + "-";
			tmp = String.valueOf(date.getDayOfMonth());
			tmp = tmp.length()==2?tmp:("0"+tmp);
			dateEndStr += tmp;
			if(controller.classInfo.repeat.equals("0"))
				controller.editDuration(dateStartStr,dateStartStr);
			else
				controller.editDuration(dateStartStr,dateEndStr);
		}
	}
	class InputTextDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if(isEditLocation){
				String location = ((EditText) ((Dialog) dialog).findViewById(R.id.editTextView)).getText().toString();
				controller.editLocation(location);
			}
			else{
				String name = ((EditText) ((Dialog) dialog).findViewById(R.id.editTextView)).getText().toString();
				controller.editClassName(name);
			}
		}
	}
	class TimeTimeDialogOnOkClickListener implements android.content.DialogInterface.OnClickListener {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			TimePicker timePicker = (TimePicker) ((AlertDialog)dialog).findViewById(R.id.timeStartPicker);
			String timeStartStr = "";
			String str = "";
			str = timePicker.getCurrentHour().toString();
			timeStartStr += (str.length()==2?str:("0"+str))+":";
			str = timePicker.getCurrentMinute().toString();
			timeStartStr += (str.length()==2?str:("0"+str));
			String timeEndStr = "";
			str = "";
			timePicker = (TimePicker) ((AlertDialog)dialog).findViewById(R.id.timeEndPicker);
			str = timePicker.getCurrentHour().toString();
			timeEndStr += (str.length()==2?str:("0"+str))+":";
			str = timePicker.getCurrentMinute().toString();
			timeEndStr += (str.length()==2?str:("0"+str));
			
			controller.editTime(timeStartStr,timeEndStr);
		}
	}
}
